package org.openmrs.module.simplelabentry.model;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.openmrs.Concept;
import org.openmrs.ConceptAnswer;
import org.openmrs.ConceptSet;
import org.openmrs.Privilege;
import org.openmrs.api.context.Context;
import org.openmrs.module.hospitalcore.BillingService;
import org.openmrs.module.hospitalcore.model.BillableService;
import org.openmrs.module.simplelabentry.SimpleLabEntryService;

public class LabTests {

    private List<BillableService> items;

    public LabTests() {
        //System.out.println("inside labtest constructor");
        BillingService bs = (BillingService) Context.getService(BillingService.class);
        SimpleLabEntryService ls = (SimpleLabEntryService) Context.getService(SimpleLabEntryService.class);
        List<Concept> supportedLabConcepts = new ArrayList<Concept>();
        for (Concept concept : ls.getSupportedLabSets()) {
            //System.out.println("_____________________concept = " + concept);
            Privilege p = new Privilege("View " + concept.getName(), "View " + concept + " lab");

            if (Context.getUserService().getPrivilege("View " + concept.getName()) == null) {
                Context.getUserService().savePrivilege(p);
            }
            supportedLabConcepts.add(concept);//hematology,biology
        }
        Map<Concept,Concept> allTestsMap = new HashMap<Concept,Concept>();
        ArrayList<Concept> allTests = new ArrayList<Concept>();
        for (int i = 0; i < supportedLabConcepts.size(); i++) {
            Collection<ConceptAnswer> answers = ((Concept) supportedLabConcepts.get(i)).getAnswers();
            for (ConceptAnswer concept : answers) {
                Concept c = concept.getAnswerConcept();
                if (c.getConceptClass().getName().equalsIgnoreCase("LabSet")) {
                    Collection<ConceptSet> cSets = c.getConceptSets();
                    for (ConceptSet concept1 : cSets) {
                        //System.out.println("concept = "+concept1.getConcept().getName() + " conceptset = "+concept1.getConceptSet().getName());
                        Concept c1 = concept1.getConcept();
                        if (c1.getConceptClass().getName().equalsIgnoreCase("Test")) {
                            if(!allTestsMap.containsKey(c))
                            {
                                allTestsMap.put(c1, supportedLabConcepts.get(i));
                            }
                            //System.out.println("adding testname = " + c1.getName() + " id = " + c.getConceptId());
                        }
                    }
                } else {

                    if (c.getConceptClass().getName().equalsIgnoreCase("Test")) {
                        if(!allTestsMap.containsKey(c))
                        {
                            allTestsMap.put(c, supportedLabConcepts.get(i));
                            allTests.add(c);
                            //System.out.println("adding test id = " + c.getConceptId());
                        }
                    }
                }

            }
        }
        List<BillableService> newitems = new ArrayList();
        for (int i = 0; i < allTests.size(); i++) {
            //System.out.println("_____________conceptid = " + allTests.get(i).getConceptId() + " parentid "+allTestsMap.get(allTests.get(i)).getConceptId());
        	BillableService ser = bs.getServiceByConceptId(allTests.get(i).getConceptId() );
            if(ser!=null)
            {
                //System.out.println("ser is disabled "+ser.getDisabled());
                newitems.add(ser);
            }
            
        }
        items = newitems;
    }

    /**
     * @return the items
     */
    public List<BillableService> getItems() {
        //System.out.println("getting items size = "+items.size());
        return items;
    }

    /**
     * @param items the items to set
     */
    public void setItems(List<BillableService> items) {
        this.items = items;
        System.out.println("setting items size = "+items.size());
    }
}
